package game.DecuryMan
{
	import flash.display.Loader;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.ui.Mouse;
	
	import mx.controls.Alert;
	
	import org.osmf.media.IMediaGateway;
	
	import spark.core.SpriteVisualElement;

	public class LevelSetSprite extends SpriteVisualElement
	{
		public static var levelArr:Array = new Array(5);
		public static var levelArrImgs:Array = new Array(5);
		public static var manArr:Array = new Array(5);
		
		public static var IMGDIR:String;
		
		//显示鼠标经过时的方框位置(蓝色方框)
		public static var showPos:Sprite = new Sprite();
		
		public static var currentDrag:LevelSprite = new LevelSprite();
		
		public function LevelSetSprite()
		{
			this.buttonMode = true;	
			this.useHandCursor = true;
			this.width = 55 * 5;
			this.height = 120;
			
			//初始化双方的棋子 只生成对象
			for(var i:int=0; i<5; i++){
				levelArr[i] = new LevelSprite();
				levelArrImgs[i] = new Loader();
				
				manArr[i] = new LevelSprite();
			}
			
			//初始化

			
			//添加蓝色方框 跟随鼠标
			this.addEventListener(MouseEvent.MOUSE_MOVE,move);
			
			//蓝色，显示当前鼠标所在的方框
			showPos.graphics.lineStyle(2,0x0000ff);
			showPos.graphics.drawRect(0,0,50,50);
			showPos.y = 5;
			
		}
		
		public function initLevel(p:int):void{
			//Alert.show("更新分配等级窗口：" + p);
			IMGDIR = ChessMain.IMGDIR_ARR[p];
			
			
			for(var i:int = 0; i<5; i++){
				levelArrImgs[i].load(new URLRequest(IMGDIR + ChessMain.imgArr[i+1]));
				levelArr[i].addChild(levelArrImgs[i]);
				levelArr[i].x = i*(50+5)+5;
				levelArr[i].y = 5;
				levelArr[i].index = 0; // 为0，表示此等级还没有分配
				this.addChild(levelArr[i]);
				
				var img:Loader = new Loader();
				img.load(new URLRequest(IMGDIR +(i+1)+ ChessMain.imgArr[0]));
				manArr[i].addChild(img);
				
				manArr[i].x = i*(55)+5;
				manArr[i].y = 60;
				manArr[i].index = i+1;
				//manArr[i].level = 0; //每个棋子的等级初试为0；
				manArr[i].scaleX = 5.0/6.0;
				manArr[i].scaleY = 5.0/6.0;
				this.addChild(manArr[i]);
			}
			
			
		}
		
		public function startLevelSet():void{
			for(var i:int=0; i<5; i++){
				manArr[i].y = 60;
				//开始拖到事件
				manArr[i].addEventListener(MouseEvent.MOUSE_DOWN, drag);
				//停止拖动事件
				manArr[i].addEventListener(MouseEvent.MOUSE_UP,updrag);
				//this.addEventListener(MouseEvent.MOUSE_UP,currentupdrag);
				this.addChild(manArr[i]);
			}
		}
		
		public function stopLevelSet():void{
			ChessMain.game.levelResetButton.label = "当前不可分配等级";
			//ChessMain.game.levelResetButton.width = 100;
			ChessMain.game.levelResetButton.enabled = false;
				
			for(var i:int=0; i<5; i++){
				manArr[i].removeEventListener(MouseEvent.MOUSE_DOWN, drag);
				manArr[i].removeEventListener(MouseEvent.MOUSE_UP,updrag);
				levelArr[i].removeEventListener(MouseEvent.MOUSE_DOWN, drag2);

			}
		}
		
		public function drag(evt:MouseEvent):void{
			var target:LevelSprite = evt.target.parent;
			this.addChild(target); //把选中的棋子，放到最上层
			target.x = this.mouseX - 25;
			target.y = this.mouseY - 25;
			target.startDrag();
			currentDrag = target;
		}
		
		public function updrag(evt:MouseEvent):void{
			var col:int = this.mouseX/55; //减去棋盘的偏移量
			//所拖动的棋子
			var target:LevelSprite = evt.target.parent;
			//Alert.show("" + col);
			if(this.mouseY>0 && this.mouseY<60 && col >=0 && col <=4){
				//Alert.show(ChessMain.imgArr + evt.target.parent.index + ChessMain.imgArr[col]);
				//加载复合图片
				levelArrImgs[col].load(new URLRequest(IMGDIR + target.index + ChessMain.imgArr[col+1]));
				levelArrImgs[col].scaleX = 5.0/6.0;
				levelArrImgs[col].scaleY = 5.0/6.0;
				
				
				
				//此处已分配棋子。 要将原棋子放回原处
				if(levelArr[col].index != 0){
					var oldMain:LevelSprite = manArr[levelArr[col].index-1];
					//如果直接是从下面托上去的
					if(target.level == 0){
						this.addChild(oldMain);
						oldMain.x =(oldMain.index - 1) * (50+5)+5;
						oldMain.y = 60;
						oldMain.level = 0;
						changeLevel(oldMain.index-1,0);
					}else{// 如果是  两个棋子对换等级
						
						//先更改原来的图片
						//Alert.show( levelArr[col].index + " " +target.level);
						levelArrImgs[target.level-1].load(new URLRequest(IMGDIR + levelArr[col].index + ChessMain.imgArr[target.level]));
						levelArrImgs[target.level-1].scaleX = 5.0/6.0;
						levelArrImgs[target.level-1].scaleY = 5.0/6.0;
						levelArr[target.level-1].addEventListener(MouseEvent.MOUSE_DOWN, drag2);
						
						//更改当前位置图片
						changeLevel(levelArr[col].index-1, target.level);
						levelArr[target.level-1].index = levelArr[col].index;
						oldMain.level = target.level;
					}
					
				}
				
				//更改棋盘上的棋子
				changeLevel(target.index-1,col+1);
				
				//复合图片要  保存一个index， 指向那个棋子
				levelArr[col].index = target.index;
				target.level = col + 1;
				
				//复合棋子
				levelArr[col].addEventListener(MouseEvent.MOUSE_DOWN, drag2);
				
				this.removeChild(target); //复合后， 删除拖动的棋子
				
			}else{
				//如果拖动的区域不对，就返回原处
				target.x = (target.index-1)*55 + 5;
				target.y = 60;
				
				//如果棋子已经分配，由于棋子已拖走， 当前等级未分配
				if(target.level > 0)
					levelArr[target.level-1].index = 0;
				//当前棋子未分配
				target.level = 0;
				changeLevel(target.index-1,0);
			}
				
			target.stopDrag();
		}
		
		//点击复合棋子，分配过的棋子.  进行分离
		public function drag2(evt:MouseEvent):void{
			//这个target为点击的复合棋子
			var target:LevelSprite = evt.target.parent;
			//
			var col:int = this.mouseX / 55;
			if(col >=0 && col <=4){
				
				//重新加载， level图片.  每次level重新加载都对应棋盘上棋子  等 级的变化
				// 这里的 col就代表等级。　　index是棋子
				levelArrImgs[col].load(new URLRequest(IMGDIR + ChessMain.imgArr[col+1]));
				levelArrImgs[col].scaleX = 1;
				levelArrImgs[col].scaleY = 1;
				levelArr[col].removeEventListener(MouseEvent.MOUSE_DOWN, drag2);
				
				//只要一分离，就设置为0
				//levelArr[col].index = 0;
				
				//分离出 拖动的棋子
				this.addChild(manArr[target.index-1]);
				manArr[target.index-1].startDrag();
				manArr[target.index-1].x = this.mouseX - 25;
				manArr[target.index-1].y = this.mouseY - 25;
				
				currentDrag = manArr[target.index-1];
				
				//levelArr[col].index = 0;
				//改变等级
				changeLevel(target.index-1,0);
				levelArr[col].index = 0;
				target.level = 0;
				
				
			}
		}
		
		
		public function levelReset():void{
			for(var i:int=0; i<5; i++){
				manArr[i].x = i*(50+5)+5;
				manArr[i].y = 60;
				this.addChild(manArr[i]);
				manArr[i].level = 0;
				levelArrImgs[i].scaleX = 1;
				levelArrImgs[i].scaleY = 1;
				levelArrImgs[i].load(new URLRequest(IMGDIR + ChessMain.imgArr[i+1]));
				
				levelArr[i].index = 0; // 为0，表示此等级还没有分配
				this.addChild(levelArr[i]);
			}
			
		}
		
		public function move(evt:MouseEvent):void{
			this.addChild(showPos);
			var col:int = this.mouseX/55; //减去棋盘的偏移量
			if(this.mouseY>0 && this.mouseY<60 && col >=0 && col <=4){
				this.addChild(showPos);
				showPos.x = 55*col + 5;
			}else
				this.removeChild(showPos);
			

			if(this.mouseX > this.width || this.mouseY >this.height || this.mouseX < 0 || this.mouseY < -25){
				currentDrag.stopDrag();
				currentDrag.x = (currentDrag.index-1)*55 + 5;
				currentDrag.y = 60;
			}
			
		}
		
		public function changeLevel(index:int, level:int):void{
			ChessMain.manArr[index + ChessMain.player*10].load(new URLRequest(IMGDIR + (index+1) + ChessMain.imgArr[level]));
			ChessMain.manArr[index+5 + ChessMain.player*10].load(new URLRequest(IMGDIR + (index+1) + ChessMain.imgArr[level]));
			ChessMain.manArr[index + ChessMain.player*10].level = level;
			//Alert.show(index+5 + ChessMain.player*10 + " ");
			ChessMain.manArr[index+5 + ChessMain.player*10].level = level;
		}
		
	
	}
}